package com.neusoft.databus.agent.trap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import com.neusoft.databus.agent.config.DatabusAgentProperties;

import lombok.extern.slf4j.Slf4j;

/**
 * description
 *
 * @author sunchf
 * @date 2019年3月28日 上午8:57:14
 */
@Slf4j
@ConditionalOnProperty(name = "databus.agent.snmpTrap.server.disabled", havingValue = "false", matchIfMissing = true)
@Configuration
public class SnmptrapServerConfiguration implements CommandLineRunner {

    @Autowired
    private DatabusAgentProperties properties;
    @Autowired
    private SnmptrapProcessor snmptrapProcessor;

    @Override
    public void run(String... args) throws Exception {
        this.startSnmptrapServer();
    }

    private void startSnmptrapServer() {
        SnmptrapServer server;
        try {
            server = new SnmptrapServer(this.properties.getSnmpTrap().getServer().getAddress(),
                    this.properties.getSnmpTrap().getServer().getPort(),
                    this.properties.getSnmpTrap().getServer().getThreadPoolSize(), this.snmptrapProcessor);
            server.start();
            if (SnmptrapServerConfiguration.log.isInfoEnabled()) {
                SnmptrapServerConfiguration.log.info("Snmp trap server has started!");
            }
        } catch (final Exception e) {
            SnmptrapServerConfiguration.log.error(e.getMessage(), e);
        }
    }
}
